<?php
class DB{
	private static $instance;
	private static $pdo;
	
	public function __construct(){
		$dbConfig = rareConfig::getAll('db');
		$dsn = "mysql:host={$dbConfig['default']['host']};dbname={$dbConfig['default']['dbname']}";
		self::$pdo = new PDO($dsn, $dbConfig['default']['username'], $dbConfig['default']['passwd']);
		self::$pdo->query('set names utf8');
		self::$pdo->query('CHARACTER SET utf8');
		self::$pdo->query("COLLATION_CONNECTION='utf8_general_ci'");
	}
	public static function getInstance(){
		if(empty(self::$instance)){
			self::$instance = new DB();
		}
		return self::$instance;
	}
	public function select($sql){
		return self::$pdo->query($sql);
	}
	public function del($sql){
		return self::$pdo->exec($sql);
	}
	public function update($sql){
		return self::$pdo->exec($sql);
	}
	public function insert($sql){
		return self::$pdo->exec($sql);
	}
	public function lastInsertID(){
		return self::$pdo->lastInsertID();
	}
	public function listPage($sql,$size=10,$page=1){
		$start= $size*($page-1);
		$limit= $start.','.$size;
		$sql=preg_replace("/^select/i", "SELECT SQL_CALC_FOUND_ROWS ", $sql)." LIMIT ".$limit;
		$list= $this->select($sql);
		$sql2='SELECT FOUND_ROWS()';
		$total=(int)$this->select($sql2)->fetchColumn();
		return array($list,$total);
	} 
}
?>
